package com.he.hsdyc.config.wechatmini;

import com.he.hsdyc.config.wechatmini.constant.WechatMiniConstants;
import com.he.hsdyc.config.wechatmini.model.TemplateData;
import com.he.hsdyc.config.wechatmini.model.WxMssVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Map;


@Service
@Slf4j
public class WxPushServiceImpl implements WxPushService {


    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    //用来请求微信的get和post
    @Autowired
    private RestTemplate restTemplate;
//    @Autowired
//    private RedisConfigPush redisConfig;
//    @Autowired
//    private WechatProperties wechatProperties;


    /*
     * 微信小程序推送
     * */
    @Override
    public String pushMessage(String userType, String openid, String templateId, Map<String, TemplateData> pushData) {


//        String appId = "";
//        String appSecret = "";
//        String tokenName = "";
        //获取不同端的appId、appSecret
//        if(userType.equals("parent")) {
//            appId = wechatProperties.getAppIdParent();
//            appSecret = wechatProperties.getAppSecretParent();
//            tokenName = "access_token_parent";
//        }else if(userType.equals("teacher")) {
//            appId = wechatProperties.getAppIdTeacher();
//            appSecret = wechatProperties.getAppSecretTeacher();
//            tokenName = "access_token_teacher";
//        }else {
//            appId = wechatProperties.getAppIdMaster();
//            appSecret = wechatProperties.getAppSecretMaster();
//            tokenName = "access_token_master";
//        }
        //获取access_token
        String access_token = "23_jWYiBmlAGsrQ6SGxNHjFEtwCD5HvG4AFVswV71R-QmmDIxscS-J3o7dBzk7HTe3BqjS8KiqqS-RwH-negZsbgqqNOf6Ke_vzPY7E--tEbBIptU9SypU-2_k-ysM2LyWmXinekmfTOVVxFIY0GMQfADACIM";
//        String access_token = getAccess_token(appId, appSecret,tokenName);
        String url = WechatMiniConstants.WECHAT_MINI_TEMPLATE_PUSH_URL +"?access_token="+ access_token;

        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo(openid,templateId,"index","FORMID",pushData);
        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        log.error("小程序推送结果={}", responseEntity.getBody());
        //小程序推送结果={"errcode":41028,"errmsg":"invalid form id hint: [w30Xra04914108]"}---------formId无效，还是需要前端在表单获取
        return responseEntity.getBody();
    }

    /*
     * 获取access_token
     * */
//    public String getAccess_token(String appId, String appSecret,String tokenName) {
//        String accessToken = "";
//        Jedis jedis = redisConfig.getJedis();
//        try {
//            //先去redis看能否命中
//            accessToken = jedis.get(tokenName);
//            if(null != accessToken) {
//                return accessToken;
//            }else {
//                //未命中直接去微信重新获取access_token
//                String url = WechatMiniConstants.WECHAT_MINI_GET_ACCESS_TOEN_URL +
//                        "&appid=" + appId + "&secret=" + appSecret;
//                String json = restTemplate.getForObject(url, String.class);
//                accessToken = new Gson().fromJson(json, AccessToken.class).getAccess_token();
//                //重新保存进redis并设置过期时间
//                jedis.setex(tokenName, 5400, accessToken);
//                return accessToken;
//            }
//        } finally {
//           returnToPool(jedis);
//        }
//    }
    //请求地址：https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx857af4e7b0451188&secret=8f66689920b89c4c5c2df3249cd7545e
    //返回结果:200	OK
    //Connection: close
    //Date: Thu, 11 Jul 2019 09:39:35 GMT
    //Content-Type: application/json; encoding=utf-8
    //Content-Length: 194
    //{
    //    "access_token": "23_EKOrz_Nlb5ZjDUDXl8CJbyNUzldiZcKhNJ07itZntxWl74bGWX2QmIsw1eTDm23IjQx3kTjD-rHZdAgjWkzX92aj0hepx_QNhgxgTzsrNTx0lSLNcXA-BFH4ir5RTdcU7n3Dynw5Nfs1SnzkXQPeAGABUT",
    //    "expires_in": 7200
    //}

//    private void returnToPool(Jedis jedis) {
//        if(jedis != null) {
//            jedis.close();
//        }
//    }



}
